package websocket.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import websocket.dto.ResultDTO;
import websocket.service.ConnectionManageService;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author neiruijie
 * @date 2022年9月16日10:49:07
 * 连接管理Controller
 */
@RestController
@RequestMapping("/api/connection-manage")
@Api(tags = "连接管理Controller")
public class ConnectionManageController {

    @Autowired
    private ConnectionManageService connectionManageService;
    static Map<String,String> portMap=new ConcurrentHashMap<>();


    @GetMapping("/get-ports")
    @ApiOperation(value = "获取所有已打的串口")
    public ResultDTO getSerialPortList() {
        return connectionManageService.getSerialPortList();
    }

    @GetMapping("/connect-port")
    @ApiOperation(value = "开始连接串口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "wrjPort", value = "无人机串口", required = true),
            @ApiImplicitParam(name = "hxPort", value = "航向站串口", required = true),
            @ApiImplicitParam(name = "cldPort", value = "测量点窗口", required = true),
    })
    public ResultDTO connectSerialPort(@RequestParam(name = "wrjPort") String wrjPort,
                                       @RequestParam(name = "hxPort") String hxPort,
                                       @RequestParam(name = "cldPort") String cldPort) {
        return connectionManageService.connectSerialPort(wrjPort,hxPort,cldPort);
    }


    /**
     * 下面新增三个
     */
    @GetMapping("/connect-port/wrj")
    @ApiOperation(value = "开始连接串口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "wrjPort", value = "无人机串口", required = true),
    })
    public ResultDTO connectSerialPort1(@RequestParam(name = "wrjPort") String wrjPort) {
        portMap.put(wrjPort,wrjPort);
        return connectionManageService.connectPortWrj(wrjPort);
    }

    @GetMapping("/connect-port/hx")
    @ApiOperation(value = "开始连接串口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "hxPort", value = "航向站串口", required = true)
    })
    public ResultDTO connectSerialPort2(
                                       @RequestParam(name = "hxPort") String hxPort) {
        portMap.put(hxPort,hxPort);
        return connectionManageService.connectPorthx(hxPort);
    }


    @GetMapping("/connect-port/cld")
    @ApiOperation(value = "开始连接串口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "cldPort", value = "测量点窗口", required = true)
    })
    public ResultDTO connectSerialPort3(@RequestParam(name = "cldPort") String cldPort) {
        portMap.put(cldPort,cldPort);
        return connectionManageService.connectPortcld(cldPort);
    }


}
